package com.kly.sc.api.application.helper;


import com.fqgj.exception.common.ApplicationException;
import com.kly.sc.api.application.vo.UserAccessInfo;
import com.kly.sc.api.infras.app.RequestBasicInfo;
import com.kly.sc.api.infras.enums.RestCodeEnum;

/**
 * @author poyangchen
 */
public class RequestLocalInfo {

    private static final ThreadLocal<Boolean> currentEnvIsTest = new ThreadLocal<>();
    private static final ThreadLocal<UserAccessInfo> currentUser = new ThreadLocal<>();
    private static final ThreadLocal<RequestBasicInfo> currentRequestBasicVO = new ThreadLocal<>();


    public static void clearThreadLocal() {
        currentUser.remove();
        currentEnvIsTest.remove();
        currentRequestBasicVO.remove();
    }

    public static void putCurrentUser(UserAccessInfo user) {
        if (user == null) {
            throw new ApplicationException(RestCodeEnum.USER_NOT_EXIST_ERROR);
        }

        RequestLocalInfo.currentUser.set(user);
    }

    public static UserAccessInfo getCurrentUser() {
        return currentUser.get();
    }


    public static void putCurrentEnvIsTest(Boolean env) {

        RequestLocalInfo.currentEnvIsTest.set(env);
    }

    public static RequestBasicInfo getRequestBasicInfo() {
        RequestBasicInfo requestBasicInfo = currentRequestBasicVO.get();
        return requestBasicInfo;
    }

    /**
     * 当前请求的app参数信息
     *
     * @param requestBasicVO
     */
    public static void putCurrentRequestBasicVO(RequestBasicInfo requestBasicVO) {
        if (requestBasicVO == null) {
            return;
        }

        RequestLocalInfo.currentRequestBasicVO.set(requestBasicVO);
    }

}
